import Foundation

extension String {
    func toFloat() -> Float? {
        // 移除所有空白字符
        let cleanedString = self.trimmingCharacters(in: .whitespaces)
        
        // 处理百分比
        if cleanedString.hasSuffix("%") {
            if let number = Float(cleanedString.dropLast()) {
                return number / 100.0
            }
            return nil
        }
        
        // 处理 px、pt、em 等单位
        let units = ["px", "pt", "em", "rem"]
        for unit in units {
            if cleanedString.hasSuffix(unit) {
                let numberString = cleanedString.replacingOccurrences(of: unit, with: "")
                return Float(numberString)
            }
        }
        
        // 尝试直接转换数字
        return Float(cleanedString)
    }
} 